package com.weng.coder.service.common;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileWriter;
import java.io.InputStream;  
import java.io.InputStreamReader;

/**
 *项目路径转换，如项目名，公司编码等
 *
 * @version 
 * @author wengzhonghui  2017年7月24日 上午10:49:52
 * 
 */
public final class ProjectPathTranslate {
 
	private String path;  
    private Map<String,String> contentTranMap;
    private Map<String,String> menuTranMap;
    
    
    public ProjectPathTranslate(String path, String companyName_old, String companyName_new,
    		 String project_old, String project_new, String projectName_old, String projectName_New
    		 ,String oldJdbc,String newJdbc) {  

    	this.path = path;
    	buildTranslateProperties(companyName_old,  companyName_new,
       		  project_old,  project_new, projectName_old,  projectName_New,oldJdbc,newJdbc);
    }
  
    /**
     * 构建替换属性
     * 
     * @param companyName_old
     * @param companyName_new
     * @param project_old
     * @param project_new
     * @param projectName_old
     * @param projectName_New
     */
    private void buildTranslateProperties(String companyName_old, String companyName_new,
   		 String project_old, String project_new, String projectName_old, String projectName_New
   		,String oldJdbc,String newJdbc){
    	Map<String,String> tranMap = new HashMap<String, String>();
    	tranMap.put("<groupId>com."+companyName_old+"</groupId>", "<groupId>com."+companyName_new+"</groupId>");
    	tranMap.put("<artifactId>"+project_old+"</artifactId>", "<artifactId>"+project_new+"</artifactId>");
    	tranMap.put("<groupId>com."+companyName_old+"</groupId>", "<groupId>com."+companyName_new+"</groupId>");
    	tranMap.put(project_old+"-", project_new + "-");
    	tranMap.put("."+companyName_old+"."+project_old+".", "."+companyName_new+"."+project_new+".");
    	tranMap.put("com."+companyName_old + ";", "com."+companyName_new + ";");
    	tranMap.put("www."+project_old+".org", "www."+project_new+".org");
    	tranMap.put(projectName_old, projectName_New);
    	tranMap.put(oldJdbc, newJdbc);
    	
    	Map<String,String> menuTranMap = new HashMap<String,String>();
    	menuTranMap.put(companyName_old, companyName_new);
    	menuTranMap.put(project_old, project_new);
    	menuTranMap.put(project_old+ "-common", project_new+ "-common");
    	menuTranMap.put(project_old+ "-dao", project_new+ "-dao");
    	menuTranMap.put(project_old+ "-service", project_new+ "-service");
    	menuTranMap.put(project_old+ "-web", project_new+ "-web");
    	menuTranMap.put(project_old+ "</finalName>", project_new+ "</finalName>");
    	this.contentTranMap = tranMap;
    	this.menuTranMap = menuTranMap;
    }
    /**
     * 文件路径名称替换
     * 
     * @author wengzhonghui 2017年7月24日
     */
    public void pathTranslate() {  
        File file = new File(path);  
        opeationDirectory(file);  
    }  
  
    public void opeationDirectory(File dir) {  
    	System.out.println(dir.getAbsolutePath());
        File[] files = dir.listFiles(); 
        if(files==null) return;
        for (int i = 0; i < files.length; i++) {  
            File f = files[i];  
            if (f.isDirectory()){
//            	System.out.println(f.getAbsolutePath());
            	String fileName = f.getName();
            	String newFileName = fileName;
            	boolean isChange = false;
            			
	            for(Entry<String,String> entry:menuTranMap.entrySet()){
	            	if(fileName.equalsIgnoreCase(entry.getKey())){
	            		newFileName = fileName.replace(entry.getKey(), entry.getValue());
	            		isChange = true;
	            		break;
                	}
	        	}
	            if(isChange){
	            	File destFile = new File(f.getParent() + File.separator + newFileName);
	            	try {
	            		f.renameTo(destFile);
					} catch (Exception e) {
						e.printStackTrace();
					}
	            	
	            	opeationDirectory(destFile);// 递归。 
	            }else{
	            	 opeationDirectory(f);//递归。  
	            }
            }else if (f.isFile()) {
            	operationFile(f); 
            }
        }  
    }  
  
    /**
     * 处理文件
     * 
     * @param file
     */
    public void operationFile(File file) {  
        try {  
            String fileName = file.getName();
            if(fileName.indexOf(".xml")<=0 && fileName.indexOf(".XML")<=0
            		&& fileName.indexOf(".java")<=0 && fileName.indexOf(".JAVA")<=0
            		&& fileName.indexOf(".jsp")<=0 && fileName.indexOf(".JSP")<=0
            		&& fileName.indexOf(".tld")<=0 && fileName.indexOf(".TLD")<=0
            		&& fileName.indexOf(".properties")<=0 && fileName.indexOf(".PROPERTIES")<=0){
            	return;
            }
            System.out.println(file.getAbsolutePath());
            InputStream is = new FileInputStream(file);  
            BufferedReader reader = new BufferedReader(  
                    new InputStreamReader(is));
            StringBuilder str = new StringBuilder();
            String strTemp = null;
            while (true) {  
                strTemp = reader.readLine();
                if(strTemp == null){
                	break;  
                }
            	str.append(strTemp);
                str.append("\n");
            }
            
            String datas = str.toString();
            if(datas.length()>0){
            	for(Entry<String,String> entry:contentTranMap.entrySet()){
            		if(datas.indexOf(entry.getKey())>=0){
            			datas = datas.replaceAll(entry.getKey(), entry.getValue());
            		}
            	}
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write(datas);  
            
            is.close();  
  
            writer.flush();  
            writer.close();  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    } 
    
 
//    public static void main(String[] args) {
//    	String targetPath = "E:\\data\\load\\excel\\unzipTest\\tarPath";
//    	
//    	String companyName_old = "comba";
//    	String companyName_new = "weng";
//    	String project_old = "baseC1";
//    	String project_new = "knowtree_base";
//    	String projectName_old = "代码基础平台";
//    	String projectName_New = "基础信息管理";
//    	String oldJdbc = "jdbc:mysql://172.16.18.41:3306/basec1";
//    	String newJdbc = "jdbc:mysql://172.16.18.41:3306/testbasec1";
//    	ProjectPathTranslate tran = new ProjectPathTranslate(targetPath, companyName_old, companyName_new
//    			, project_old, project_new, projectName_old, projectName_New,oldJdbc,newJdbc);
//    	tran.pathTranslate();
//    }
}
